package model.search;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.TreeSet;

public class Stopwords {
	HashMap<String, Boolean> stopwords;

	private String file = model.MainModel.applicationRoot
			+ "/settings/stopwords.txt";

	public Stopwords() {
		stopwords = new HashMap<String, Boolean>();
		loadWords();
	}
	
	public Stopwords(String file) {
		stopwords = new HashMap<String, Boolean>();
		this.file = file;
		if (file != "")
			loadWords();
	}

	private void loadWords() {
		FileReader fr;
		try {
			fr = new FileReader(new File(file));
			BufferedReader br = new BufferedReader(fr);
			while (br.ready()) {
				String word = br.readLine();
				addWord(word);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void addWord(String word) {
		stopwords.put(word, true);
	}

	public boolean contains(String word) {
		return stopwords.containsKey(word);
	}

	public TreeSet<String> manipulate(String[] words) {
		TreeSet<String> keywords = new TreeSet<String>();
		for (int i = 0; i < words.length; i++) {
			if (!stopwords.containsKey(words[i]))
				keywords.add(words[i]);
		}
		return keywords;
	}
}
